package site.wanjiahao.handler.server;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import site.wanjiahao.message.request.CreateGroupRequestMessage;
import site.wanjiahao.message.response.CreateGroupResponseMessage;
import site.wanjiahao.session.SessionFactory;

@ChannelHandler.Sharable
public class CreateGroupRequestMessageHandler extends SimpleChannelInboundHandler<CreateGroupRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, CreateGroupRequestMessage msg) throws Exception {
        boolean success = SessionFactory.getGroupSession().createGroup(msg.getGName());
        CreateGroupResponseMessage createGroupResponseMessage = new CreateGroupResponseMessage(success, success ? "创建组成功" : "创建组失败");
        ctx.writeAndFlush(createGroupResponseMessage);
    }
}
